package com.sunday.common.mq.kafka.study.spring.e16_Handling_Exceptions;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.listener.KafkaListenerErrorHandler;
import org.springframework.kafka.listener.ListenerExecutionFailedException;
import org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter;
import org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
public class MyKafkaListenerErrorHandler implements KafkaListenerErrorHandler {

    @Override
    public Object handleError(Message<?> message, ListenerExecutionFailedException exception, Consumer<?, ?> consumer) {
        return handleError(message, exception);
    }

    /**
     * {@link RecordMessagingMessageListenerAdapter#onMessage(ConsumerRecord, Acknowledgment, Consumer)}.
     * {@link MessagingMessageListenerAdapter#handleResult(Object, Object, Object)}
     */
    @Override
    public Object handleError(Message<?> message, ListenerExecutionFailedException exception) {
        log.error("{}", message);
        log.error(exception.toString(), exception);
        return "FAILED";
    }
}
